{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "593694ba",
   "metadata": {},
   "source": [
    "This code responsibility it to provide short review of the movie provided by the user through movie url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6602f2fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from bs4 import BeautifulSoup\n",
    "import requests\n",
    "from IPython.display import Markdown, display\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67696a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
    "}\n",
    "\n",
    "def fetch_website_contents(url):\n",
    "    \"\"\"\n",
    "    Return the title and contents of the website at the given url;\n",
    "    truncate to 2,000 characters as a sensible limit\n",
    "    \"\"\"\n",
    "    response = requests.get(url, headers=headers)\n",
    "    soup = BeautifulSoup(response.content, \"html.parser\")\n",
    "    title = soup.title.string if soup.title else \"No title found\"\n",
    "    if soup.body:\n",
    "        for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
    "            irrelevant.decompose()\n",
    "        text = soup.body.get_text(separator=\"\\n\", strip=True)\n",
    "    else:\n",
    "        text = \"\"\n",
    "    return (title + \"\\n\\n\" + text)[:2_000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2bf6ddac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "API key found and looks good so far!\n"
     ]
    }
   ],
   "source": [
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Check the key\n",
    "\n",
    "if not api_key:\n",
    "    print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
    "elif not api_key.startswith(\"sk-proj-\"):\n",
    "    print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
    "elif api_key.strip() != api_key:\n",
    "    print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
    "else:\n",
    "    print(\"API key found and looks good so far!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "65856436",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt=\"\"\"\n",
    "you are a funny movie review assistant. Provide a short review of the movie based on the plot, characters, and overall experience.\n",
    "\"\"\"\n",
    "\n",
    "user_prompt=\"\"\"\n",
    "Here is the website content.Please provide a review of the movie based on the plot, characters, and overall experience.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "00d8b154",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_movie_review(url):\n",
    "    \"\"\"\n",
    "    Get a movie review from the given url\n",
    "    \"\"\"\n",
    "    contents = fetch_website_contents(url)\n",
    "    messages = [\n",
    "       {\"role\": \"system\", \"content\": system_prompt},\n",
    "       {\"role\": \"user\", \"content\": user_prompt + contents} \n",
    "    ]\n",
    "\n",
    "    openai = OpenAI()\n",
    "    \n",
    "    response = openai.chat.completions.create(\n",
    "        model=\"gpt-4o-mini\",\n",
    "        messages=messages\n",
    "    )\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "165067d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'**Review: Manikarnika: The Queen of Jhansi**\\n\\nAre you ready for a historical drama where the real MVP is a queen who could probably out-warrior a dozen Vikings while simultaneously knitting a cardigan? Look no further than *Manikarnika: The Queen of Jhansi*! \\n\\nKangana Ranaut struts onto the screen as Rani Lakshmibai, and let me tell you, she’s not just fighting for her kingdom; she’s also fighting against the tyranny of budget constraints. The plot is as grand as it gets, weaving a tale of heroism against the British Raj with the subtlety of a cannonball to the chest. And who doesn’t love a film that kicks off with a sword fight and a dramatic narrative from Amitabh Bachchan? \\n\\nThe characters range from the undeniably fierce Rani Lakshmibai to a cast of supporting characters whose motivations occasionally get lost in the melee—kind of like losing your left sock in the dryer. Yet, every character adds a dash of flavor to the chaos, like too much salt on popcorn: it’s not quite balanced, but you enjoy it nonetheless!\\n\\nVisually, the movie is eye candy, serving up epic battles and sweeping landscapes like an Instagram influencer’s best highlights reel—only with less avocado toast. However, the pacing sometimes resembles a slow horse in a chariot race; you’re eager for the next epic moment, but occasionally find yourself waiting as “who’s that character again?” runs through your mind.\\n\\nOverall, *Manikarnika* is a valiant effort filled with action, drama, and enough emotional turmoil to fill a bath. It’s like watching a really intense game of chess where the pieces randomly start swinging swords at each other. So, if you have a couple of hours to spare and are in the mood for an extravaganza that’s equal parts history and high drama, grab your snacks and prepare to be swept into the fiery heart of 19th-century India. Just don’t forget your battle armor—you might need it if you try to argue with this film’s unexpected twists!'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_movie_review(\"https://en.wikipedia.org/wiki/Manikarnika:_The_Queen_of_Jhansi\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b5308664",
   "metadata": {},
   "outputs": [],
   "source": [
    "def review_movie(url):\n",
    "    review_movie= get_movie_review(url)\n",
    "    display(Markdown(review_movie))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "02bdc635",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "**Movie Review: Manikarnika: The Queen of Jhansi**\n",
       "\n",
       "If you ever wanted to see a royal makeover of the action genre, “Manikarnika: The Queen of Jhansi” is here to provide just that, sprinkled with a dash of historical drama and a pinch of women power! \n",
       "\n",
       "**Plot:** The film chronicles the life of Rani Lakshmibai — a fierce monarch who fought against British colonial rule in the 1850s. It’s like “Braveheart” but with a lot more saris and a lot less Mel Gibson yelling, \"Freedom!\" The plot ambitiously attempts to juggle valor, heartbreak, and heroic sacrifice, all while our heroine manages to ride horses, dodge arrows, and somehow still find time to look flawless.\n",
       "\n",
       "**Characters:** Kangana Ranaut shines as the titular queen, embodying strength and charisma, giving such a powerful performance that it might just inspire you to don your own crown and lead a rebellion. The supporting cast, featuring notable actors like Jisshu Sengupta and Ankita Lokhande, add flavor, though sometimes it feels like they’re just there to nod in support while Kangana steals the show—like that one friend who always shows up looking fabulous no matter the occasion.\n",
       "\n",
       "**Overall Experience:** The visuals are a feast for the eyes, with grand battle scenes that make you feel like the world might just end if you blink. The music complements the intensity, though sometimes it feels like it’s trying a bit too hard to evoke emotions, like that overly enthusiastic friend at a party who just can’t stop dancing. \n",
       "\n",
       "In summary, while “Manikarnika” may falter on some historical accuracy and pacing, it delivers an exhilarating experience that celebrates bravery and resilience. Just make sure to keep some popcorn handy, because there’s enough drama here to keep you munching for the full 150 minutes! Great for history buffs, fans of epic storytelling, or anyone in need of a good dose of empowerment, just don’t expect it to go easy on the 'drama meter'!"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "review_movie(\"https://en.wikipedia.org/wiki/Manikarnika:_The_Queen_of_Jhansi\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f81ca16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
